import {action, observable} from "mobx";
import {Bubble, Toast, Tools} from "weaver-mobile";
import {ListViewPage, SearchAdvanced} from "weaver-mobile-page";
import {programAuditListApi, programAuditOperationApi} from "../../api/performance/programAuditListApi";

const getLabel = Tools.getLabel;

/**
 * 待审核方案
 * jiansong 2019/11/19
 */
export default class ProgramAuditStore {

    @observable list = new ListViewPage.Store({route: "/performance/programAudit"});
    @observable search = new SearchAdvanced.Store({route: "/performance/programAudit"});

    @action("初始化操作")
    doInit = () => {
        this.search.form.resetFields();
        this.getTableDatas();
        this.getCondition();
    }

    @action("更新项目名称条件")
    updateFormValue = (params) => {
        for (let key in params) {
            this.search.form.updateField(key, params[key]);
        }
    }

    @action("获得高级搜索表单数据")
    getCondition = () => {
        let options = {
            method: "GET",
            url: "/api/workrelate/performance/programAuditListCondition",
        };
        //设置后端返回条件字段
        this.search.setState({responseKey: "condition"})
        this.search.getData(options);
    };

    @action("渲染table数据")
    getTableDatas = () => {
        let params = this.search.form.formParams || {};
        programAuditListApi(params).then(action(res => {
            if (res.api_status) {
                this.list.clearData();
                this.list.selectNone();
                const {datas: dataKey} = res;
                this.list.getData({dataKey});
            } else {
                Bubble.error(res.msg || getLabel(508818, "接口调用失败！"))
            }
        }));
    };

    @action("待审核方案审核-操作")
    programAuditOperation = (params) => {
        Toast.loading('Loading...', 0);
        programAuditOperationApi(params).then(action(res => {
            if (res.api_status) {
                if (!res.hasRight) {
                    Bubble.error(getLabel(2012, "对不起，您暂时没有权限！"))
                } else {
                    this.list.selectNone();
                    this.list.clearData();
                    this.list.getData();
                    Bubble.success(getLabel('30700', '操作成功'))
                    if (window.RefreshAccessNum && typeof window.RefreshAccessNum === "function") {
                        window.RefreshAccessNum();
                    }
                }
            } else {
                Bubble.error(res.msg || getLabel(508818, "接口调用失败！"))
            }
            Toast.hide();
        }));
    };

}